package com.pz.cloud.framework.volunteerTeam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.Assert;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.base.service.BaseServiceImpl;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.business.volunteerTeam.entity.VolunteerTeam;
import com.pz.cloud.framework.business.volunteerTeam.entity.VolunteerTeamUser;
import com.pz.cloud.framework.business.volunteerTeam.service.IVolunteerTeamService;
import com.pz.cloud.framework.business.volunteerTeam.service.IVolunteerTeamUserService;
import com.pz.cloud.framework.business.workflow.annotation.Workflow;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.volunteerTeam.callback.VolunteerTeamCallback;
import com.pz.cloud.framework.volunteerTeam.mapper.VolunteerTeamMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class VolunteerTeamServiceImpl extends BaseServiceImpl<VolunteerTeamMapper, VolunteerTeam> implements IVolunteerTeamService {

    @Autowired
    private CommonDao commonDao;
    @Autowired
    private IVolunteerTeamUserService volunteerTeamUserService;

    @Override
    @Workflow(callback = VolunteerTeamCallback.class)
    public boolean save(VolunteerTeam entity) {
        boolean flag = false;
        if(StringUtils.isEmpty(entity.getId())){
            if (CollectionUtils.isNotEmpty(findByName(entity.getName()))) {
                throw new CustomRuntimeException("该志愿者队伍已存在");
            }
            flag = super.save(entity);
        }else{
            flag = super.updateById(entity,true);
        }
        entity.getWorkflowModel().setHandlerId(entity.getInstructorId());

        VolunteerTeamUser volunteerTeamUser = volunteerTeamUserService.findByUserIdAndVolunteerTeamId( entity.getDirectorId(),entity.getId());
        if(volunteerTeamUser!=null){
            volunteerTeamUser.setIsDirector(1);
        }else{
            volunteerTeamUser = new VolunteerTeamUser();
            volunteerTeamUser.setUserId(entity.getDirectorId());
            volunteerTeamUser.setRealname(entity.getDirector());
            volunteerTeamUser.setActStatus(10);
            volunteerTeamUser.setIsDirector(1);
            volunteerTeamUser.setVolunteerTeamId(entity.getId());
        }
        volunteerTeamUserService.save(volunteerTeamUser);
        return flag;
    }

    @Override
    public IPage<VolunteerTeam> findPage(VolunteerTeam entity) {
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select t.*,s.std_no director_no from ").appendTable(entityClass).append(" t ")
                .append("left join ").appendTable(Student.class).append(" s on t.director_id = s.user_id and s.del_flag = 0 ")
                .joinCondition(entity, "t")
                .filterDataScope("t")
                .returnType(entityClass)
                .orderBy("t.id desc")
                .page(entity);
        return commonDao.findPage(sqlBuilder);
    }

    @Override
    public List<VolunteerTeam> findByName(String name) {
        LambdaQueryWrapper<VolunteerTeam> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(VolunteerTeam::getName, name);
        return list(wrapper);
    }

    @Override
    public boolean removeById(Serializable id) {
        checkUser(id);
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        idList.forEach(this::checkUser);
        return super.removeByIds(idList);
    }

    private void checkUser(Serializable id) {
        LambdaQueryWrapper<VolunteerTeamUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VolunteerTeamUser::getVolunteerTeamId, id);
        if (volunteerTeamUserService.count(wrapper) > 0) {
            throw new CustomRuntimeException("请先删除成员");
        }
    }
}
